<?php
namespace Admin\Controller;
use Think\Controller;

//文字栏目
class NewsController extends Controller{
    public function __construct(){
        parent::__construct();
        define('TABLE_NAME', 'news');
        define('TABLE_CLASS_NAME', 'news_class');
        define('MY_ACTION_NAME', '新闻');
    }

    public function index(){

    }

    public function lists(){
        $ColumnModel = M(TABLE_NAME);
        $count = $ColumnModel->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        //$Page->setConfig('header', '共'.$count.'条记录');
        //进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $ColumnModel->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function listsClass(){
        $Model = M(TABLE_CLASS_NAME);
        $count = $Model->count();
        $Page = new \Think\Page($count, 10);
        $show = $Page->show();
        //$Page->setConfig('header', '共'.$count.'条记录');
        //进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Model->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    public function add(){
        $this->display();
    }

    public function addClass(){
        $this->display();
    }

    public function insert(){
        $ColumnModel = M(TABLE_NAME);
        $ColumnModel->name = I('post.name', '');
        $ColumnModel->style = '文字';
        $ColumnModel->content = I('post.content', '', false);
        $ColumnModel->sortnum = I('post.sortnum', 1, 'int');
        $ColumnModel->createtime = date('Y-m-d H:i:s');
        $ColumnModel->updatetime = date('Y-m-d H:i:s');
        $action_result = $ColumnModel->add();
        if($action_result){
            $this->success('添加栏目成功', 'addCha', 3);
        }else{
            $this->error('添加栏目失败', '', 3);
        }
    }

    public function insertClass(){
        $Model = M(TABLE_CLASS_NAME);
        $Model->sortname = I('post.sortname', '');
        $Model->status = I('post.status', '1');
        $Model->sortnum = I('post.sortnum', 1, 'int');
        $Model->createtime = date('Y-m-d H:i:s');
        $Model->updatetime = date('Y-m-d H:i:s');
        $action_result = $Model->add();
        if($action_result){
            $this->success('添加'.MY_ACTION_NAME.'成功', 'addCha', 3);
        }else{
            $this->error('添加'.MY_ACTION_NAME.'失败', '', 3);
        }
    }

    public function edit($tid=''){
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $data = $ColumnModel->find($tid);
        $this->assign('data', $data);
        $this->display();
    }

    public function update(){
        $tid = I('post.tid');
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $ColumnModel->find($tid);
        $ColumnModel->name = I('post.name');
        $ColumnModel->content = I('post.content');
        $ColumnModel->sortnum = I('post.sortnum', 1, 'int');
        $action_result = $ColumnModel->save();
        if($action_result){
            $this->success('编辑栏目成功', 'listCha', 3);
        }else{
            $this->error('编辑栏目失败');
        }
    }

    public function deleteOne($tid=''){
        if(!$tid)exit('非法操作');
        $ColumnModel = M('Column');
        $action_result = $ColumnModel->delete($tid);
        if($action_result){
            $this->success('删除栏目成功', 'Column/listCha', 3);
        }else{
            $this->error('删除栏目失败', '', 3);
        }
    }

    public function batDel(){
        $tids = I('post.tids');
        if(empty($tids))exit;
        $tids = implode(',', $tids);
        $ColumnModel = M('Column');
        $action_result = $ColumnModel->delete($tids);
        if($action_result){
            $this->success('批量删除栏目成功', 'Column/listCha', 3);
        }else{
            $this->error('批量删除栏目失败', '', 3);
        }
    }

    public function batDelClass(){
        echo __METHOD__;
    }
}